package rd.hardcore.ui.nav 
{
	import aze.motion.eaze;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import rd.hardcore.engine.datas.streamBoard.StreamBoardEvent;
	import rd.hardcore.engine.datas.StreamBoardManager;
	import rd.hardcore.engine.tools.Tools;
	/**
	 * ...
	 * @author Romain Prache
	 */
	public class StreamNavItem extends NavItem
	{
		private var l:Loader;
		private var streamId:int
		
		public function StreamNavItem(id:int) 
		{
			super();
			
			streamId = id;
			//body = new Shape();
			body.graphics.beginFill(0x666666);
			body.graphics.drawCircle(0, 0, 3);
			body.graphics.endFill();
			//addChild(body);
			body.alpha = 0;

			selectedShape.visible = false;

			
			ctrl = new Shape();
			ctrl.graphics.beginFill(0,0);
			ctrl.graphics.drawCircle( 0, 0, 8);
			ctrl.graphics.endFill(); 
			addChildAt(ctrl, 0);
			
			if ( StreamBoardManager.instance.STATE == StreamBoardManager.STREAM_BOARD_READY )
				loadIcon();
			else
				StreamBoardManager.instance.addEventListener( StreamBoardEvent.BOARD_READY_TO_DISPLAY, loadIcon );
		}
		
		private function loadIcon ( evt : Event = null ) : void
		{
			l = new Loader();
			l.contentLoaderInfo.addEventListener( Event.COMPLETE, success);
			l.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, fail);			
			l.load( new URLRequest( Tools.macOsUrlProofing(File.applicationStorageDirectory.resolvePath(StreamBoardManager.instance.getNavMedia(streamId)).nativePath )));			addChild(l);
		}
		
		override public function select():void 
		{
			super.select();
			eaze(selectedShape).to(.5, { alpha : 0  } );
		}
		
		private function success(e:Event):void
		{
			l.x = -l.width >> 1;
			l.y = -l.height >> 1;
			
			cleanListeners();
		}
		
		private function fail(e:IOErrorEvent):void 
		{
			cleanListeners();
		}
		
		private function cleanListeners() : void
		{
			l.contentLoaderInfo.removeEventListener( Event.COMPLETE, success);
			l.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, fail);
		}
		
	}

}